//
//  AddChangeController.m
//  AntsExpress
//
//  Created by slccper on 16/1/14.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "AddChangeController.h"
#import "AntsPickView.h"

@interface AddChangeController ()
{
    AntsPickView *Pickview;
    NSMutableArray *AreaList;
    NSMutableArray *WareList;
    NSMutableArray *BranchList;
    NSInteger areaSelect;
    NSInteger WareSelect;
    NSInteger BranchSelect;
}
@property (weak, nonatomic) IBOutlet UIButton *Area;
@property (weak, nonatomic) IBOutlet UIButton *Ware;
@property (weak, nonatomic) IBOutlet UIButton *Branch;
@property (weak, nonatomic) IBOutlet UILabel *lbAddress;
//@property (weak, nonatomic) void (^animations)(NSArray *arr);
@end

@implementation AddChangeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"綁定地址修改";
    ants = [[clsAntsService alloc]init];
    ants.delegate=self;
    AreaList = [NSMutableArray array];
    WareList = [NSMutableArray array];
    BranchList = [NSMutableArray array];
    // Do any additional setup after loading the view from its nib.
}
- (void)getWebData
{
    if ([clsOtherFun CheckNetworkStatus:YES]) {
        [clsOtherFun showLoadingView:@"正在獲取數據"];
        if (!ants) {
            ants = [[clsAntsService alloc]init];
            ants.delegate=self;
        }
        [ants GetQuHuoAdress:[clsOtherFun getencodeName]];
    }
}
- (IBAction)AreaAction:(id)sender {
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            [self.Area setTitle:pSelectData forState:UIControlStateNormal];
            areaSelect =select;
            self.Area.selected=NO;
            self.Ware.selected=YES;
            [WareList removeAllObjects];
            self.Branch.selected=YES;
        }];
        Pickview = nil;
    };
    if (AreaList.count>0) {
        NSMutableArray *title = [clsArea GetTitleByArr:AreaList];
        animations(title);
    }else{
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        
        [ants GetAreaList:^(NSDictionary *dic) {
            AreaList = [clsArea GetDataByArr:[dic objectForKey:@"data"]];
            [clsOtherFun hideLoadingView];
            NSMutableArray *title = [clsArea GetTitleByArr:AreaList];
            animations(title);
    }];
    }
}
- (IBAction)WareAction:(id)sender {
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
                [self.Ware setTitle:pSelectData forState:UIControlStateNormal];
                WareSelect=select;
                self.Ware.selected=NO;
                [BranchList removeAllObjects];
                self.Branch.selected=YES;
            }
        }];
        Pickview = nil;
    };
    if (WareList.count>0) {
        NSMutableArray *title = [clsWare GetTitleByArr:WareList];
        animations(title);
    }else{
        if (self.Area.selected) {
            [clsOtherFun ShowMsg:@"請選擇區域"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        clsArea *area =[AreaList objectAtIndex:areaSelect];
        [ants GetWareList:area.AreaID action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            WareList = [clsWare GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsWare GetTitleByArr:WareList];
            animations(title);
    }];
    }
}
- (IBAction)BranchAction:(id)sender {
    void (^animations)(NSArray *arr) = ^(NSArray *arr){
        Pickview = [[AntsPickView alloc]initPickviewWithArray:arr];
        [Pickview show:^(NSString *pSelectData,NSInteger select) {
            if(![pSelectData isEqualToString:@""])
            {
                [self.Branch setTitle:pSelectData forState:UIControlStateNormal];
                BranchSelect=select;
                self.Branch.selected=NO;
            }
        }];
        Pickview = nil;
    };
    if (BranchList.count>0) {
        NSMutableArray *title = [clsBranch GetTitleByArr:BranchList];
        animations(title);
    }else{
        if(self.Ware.selected){
            [clsOtherFun ShowMsg:@"請選擇倉庫"];
            return;
        }
        [clsOtherFun showLoadingView:@"正在獲取數據..."];
        clsWare *area =[WareList objectAtIndex:WareSelect];
        [ants GetBranchList:area.WareID action:^(NSDictionary *dic) {
            [clsOtherFun hideLoadingView];
            BranchList = [clsBranch GetDataByArr:[dic objectForKey:@"data"]];
            NSMutableArray *title = [clsBranch GetTitleByArr:BranchList];
            animations(title);
    }];
    }
}
- (IBAction)SubmitAction:(id)sender {
    if (self.Area.selected) {
        [clsOtherFun ShowMsg:@"請選擇區域"];
        return;
    }
    if (self.Ware.selected) {
        [clsOtherFun ShowMsg:@"請選擇倉庫"];
        return;
    }
    if (self.Branch.selected) {
        [clsOtherFun ShowMsg:@"請選擇取貨點"];
        return;
    }
    clsArea *area =[AreaList objectAtIndex:areaSelect];
    clsWare *Ware  =[WareList objectAtIndex:WareSelect];
    clsBranch *Branch =[BranchList objectAtIndex:BranchSelect];
    NSString *mark = [NSString stringWithFormat:@"%@-%@-%@",self.Area.titleLabel.text,self.Ware.titleLabel.text,self.Branch.titleLabel.text];
    [ants UpdateQuHuoAdress:area.AreaID :Branch.BranchID :Ware.WareID :mark];
}

-(void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    [clsOtherFun hideLoadingView];
    NSDictionary *dic = [pDictionary objectForKey:@"data"];
    NSString *status = [dic objectForKey:@"status"];
    if ([status integerValue]==200) {
        if (type==Ants_GetQuHuoAdress) {
            NSDictionary *data = [dic objectForKey:@"data"];
            self.lbAddress.text = [NSString stringWithFormat:@"原取貨地址：%@",[data objectForKey:@"areaMark"]];
        }else if(type ==Ants_UpdateQuHuoAdress){
            [clsOtherFun ShowMsg:@"更新成功！"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
    }
}

@end
